// const data = { name: "chenshiyuan",age:20};

// const data = ['a','b','c']

// const proxyData = new Proxy(data,{
//   get(target,key,receiver){
<<<<<<< HEAD
//     //reflect理解:https://blog.csdn.net/SignalFire/article/details/125536375
=======
//     //reflect理解:https://blog.csdn.net/qq_39852145/article/details/114240895
>>>>>>> 53ae88b1b957cea3c9aa8cdc52ba476a034a527b
//     //只处理本身(非原型的)属性
//     const ownKeys = Reflect.ownKeys(target)
//     if(ownKeys.includes(key)){//数组方法查找是否有某个属性
//       console.log('get',key);
//     }
//     const result = Reflect.get(target,key,receiver)
//     return result //返回结果
//   },
//   set(target,key,val,receiver){
//     //重复的数据,不处理
//     if(val === target[key]){
//       return true
//     }
//     const result=Reflect.set(target,key,val,receiver)
//     console.log('set',key,val);
//     console.log('result',result)
//     return result //是否设置成功
//   },
//   deleteProperty(target,key){
//     const result = Reflect.deleteProperty(target,key)
//     console.log('delete property',key);
//     console.log('result',result);
//     return result//是否删除成功
//   }
// })

function reactive(target = {}){
  if(typeof target !== 'object' || target == null){
    //不是对象或数组，则返回
    return target
  }
  //代理配置
  const proxyConf = {
    get(target,key,receiver){
      //reflect理解:https://blog.csdn.net/SignalFire/article/details/125536375
      //只处理本身(非原型的)属性
      //receiver理解:https://blog.csdn.net/qq_43198727/article/details/130021011
      //
      const ownKeys = Reflect.ownKeys(target)
      if(ownKeys.includes(key)){//数组方法查找是否有某个属性
        console.log('get',key);
      }
      const result = Reflect.get(target,key,receiver)
      //深度监听
      //性能如何提升？访问时才添加代理
      return reactive(result) //返回结果
    },
    set(target,key,val,receiver){
      //重复的数据,不处理
      if(val === target[key]){
        return true
      }

      const ownKeys = Reflect.ownKeys(target)
      //判断时新增还是已有
      if(ownKeys.includes(key)){
        console.log('已有的 key',key);
      }else{
        console.log('新增的 key',key);
      }
      //set 设置目标的属性值
      const result=Reflect.set(target,key,val,receiver)
      console.log('set',key,val);
      console.log('result',result)
      return result //是否设置成功
    },
    deleteProperty(target,key){
      const result = Reflect.deleteProperty(target,key)
      console.log('delete property',key);
      console.log('result',result);
      return result//是否删除成功
    }
  }

  //生成代理对象
  const observed = new Proxy(target,proxyConf)
  return observed
}

const data = {
  name:'zhangsan',
  age:12,
  info:{
    city:'beijing'
  }

}

let proxyData = reactive(data)
proxyData.info.city